package plugins.lsd.swfModule
{
    import core.manager.*;
    import core.manager.popupManager.*;
    import core.manager.sceneManager.*;
    import core.manager.scriptManager.*;
    import flash.display.*;
    import flash.events.*;
    import memory.*;
    import mx.core.*;
    import view.loadings.*;
    import view.player.*;

    public class SwfModuleBase extends UIComponent
    {
        private var isScene:Boolean = true;
        private var swfPlayer:SwfPlayer;
        private var completeScript:String = "";
        private var loading_mc:LoadingWaveRota;
        private var willPopup:Boolean = false;
        private var initScript:String = "";

        public function SwfModuleBase()
        {
            ScriptManager.getInstance().addApi(ScriptName.ADD_SWF_INIT_SCRIPT, addInitScript);
            ScriptManager.getInstance().addApi(ScriptName.ADD_SWF_COMPLETE_SCRIPT, addCompleteScript);
            ScriptManager.getInstance().addApi(ScriptName.SHOW_SWF, showSwf);
            ScriptManager.getInstance().addApi(ScriptName.REMOVE_SWF, dispose);
            return;
        }// end function

        private function addCompleteScript(DisplayObjectContainer:String) : void
        {
            completeScript = completeScript + DisplayObjectContainer;
            return;
        }// end function

        private function initLoadingMc() : void
        {
            loading_mc = new LoadingWaveRota();
            loading_mc.x = Application.application.width / 2;
            loading_mc.y = Application.application.height / 2;
            Application.application.addChild(loading_mc);
            return;
        }// end function

        private function on_swf_progress(DisplayObjectContainer:ProgressEvent) : void
        {
            loading_mc.updateByProgressEvent(DisplayObjectContainer);
            return;
        }// end function

        private function showSwf(url:String, isScene:Boolean = true, willPopup:Boolean = false) : void
        {
            dispose(null);
            this.isScene = isScene;
            this.willPopup = willPopup;
            ScriptManager.getInstance().runScriptDirectly(initScript);
            swfPlayer = new SwfPlayer(url, 900, 480);
            initLoadingMc();
            swfPlayer.addEventListener(ProgressEvent.PROGRESS, on_swf_progress);
            swfPlayer.addEventListener(Event.COMPLETE, on_swf_complete);
            return;
        }// end function

        private function otherSceneInit(DisplayObjectContainer:SceneChangeEvent) : void
        {
            if (swfPlayer != null)
            {
                swfPlayer.mouseEnabled = false;
                swfPlayer.enabled = false;
            }
            return;
        }// end function

        private function addInitScript(DisplayObjectContainer:String) : void
        {
            initScript = initScript + DisplayObjectContainer;
            return;
        }// end function

        private function on_swf_complete(DisplayObjectContainer:Event) : void
        {
            if (loading_mc != null)
            {
                if (loading_mc.parent != null)
                {
                    loading_mc.parent.removeChild(loading_mc);
                }
                loading_mc = null;
            }
            if (willPopup)
            {
                CustomPopupManager.getInstance().addPopUp(swfPlayer);
                CustomPopupManager.getInstance().centerPopUp(swfPlayer);
            }
            else
            {
                this.addChild(swfPlayer);
            }
            if (isScene)
            {
                SceneManager.getInstance().dispacherJustBeforeCompleteEvent(SceneManager.getInstance().currentSceneId);
            }
            SceneManager.getInstance().addEventListener(SceneChangeEvent.COMPLETE, dispose);
            SceneManager.getInstance().addEventListener(SceneChangeEvent.INIT, otherSceneInit);
            MainSystem.getInstance().isBusy = false;
            ScriptManager.getInstance().runScriptDirectly(completeScript);
            return;
        }// end function

        private function dispose(DisplayObjectContainer:SceneChangeEvent) : void
        {
            MemoryRecovery.getInstance().gcFun(SceneManager.getInstance(), SceneChangeEvent.INIT, otherSceneInit);
            MemoryRecovery.getInstance().gcFun(SceneManager.getInstance(), SceneChangeEvent.COMPLETE, dispose);
            MemoryRecovery.getInstance().gcFun(swfPlayer, ProgressEvent.PROGRESS, on_swf_progress);
            MemoryRecovery.getInstance().gcFun(swfPlayer, Event.COMPLETE, on_swf_complete);
            if (loading_mc != null)
            {
                if (loading_mc.parent != null)
                {
                    loading_mc.parent.removeChild(loading_mc);
                }
                loading_mc = null;
            }
            if (swfPlayer != null)
            {
                swfPlayer.dispose();
                if (swfPlayer.parent != null)
                {
                    if (willPopup)
                    {
                        CustomPopupManager.getInstance().removePopUp(swfPlayer);
                    }
                    else
                    {
                        swfPlayer.parent.removeChild(swfPlayer);
                    }
                }
                swfPlayer = null;
            }
            initScript = "";
            completeScript = "";
            return;
        }// end function

    }
}
